package org.example.domain.model.entity.url.vo;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.example.infrastructure.repository.orm.redis.config.constant.RedisCacheConstant;

import java.util.Date;
import java.util.Optional;

@Data
public class UrlShortenerVo {

    @Schema(description = "分组标识")
    private String gid;

    @Schema(description = "原始链接")
    private String originUrl;

    @Schema(description = "有效期")
    private Date validDate;
    @Schema(description = "域名")
    private String domain;

    @Schema(description = "短链接")
    private String shortUri;

    @Schema(description = "完整短链接")
    private String fullShortUrl;

    @Schema(description = "有效期类型 0：永久有效 1：自定义")
    private Integer validDateType;

    /**
     * 获取短链接缓存有效期时间
     *
     */
    public long acquireCacheValidTime() {
        return Optional.ofNullable(this.validDate)
                .map(each -> DateUtil.between(new Date(), each, DateUnit.MS))
                .orElse(RedisCacheConstant.DEFAULT_CACHE_VALID_TIME);
    }

}
